package com.xxxx.spring.test.jdkTest.test03;

import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

public class $Proxy01 implements Foo {

    private InvocationHandler h;

    public $Proxy01(InvocationHandler h){
        this.h = h;
    }

    static Method hello;
    static Method say;
    static {
        try {
            hello = Foo.class.getMethod("hello");
            say = Foo.class.getMethod("say");
        } catch (NoSuchMethodException e){
            throw new NoSuchMethodError(e.getMessage());
        }
    }

    @Override
    public void hello() {
        System.out.println("我是代理方法hello()");
        try {
            // Method hello = Foo.class.getMethod("hello");
            h.invoke(this,hello,new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    @Override
    public int say() {
        System.out.println("我是代理方法say()");
        try {
            // Method say = Foo.class.getMethod("say");
            return (int) h.invoke(this,say, new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }
}
